package com.guige.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：songaw
 * @description ：
 * @copyright ：
 * @create ：2017/4/12 13:47
 */
@Configuration

public class WebConfig extends WebMvcConfigurerAdapter{

//    @Bean(name = "multipartResolver")
//    public MultipartResolver createMultipartResolver(){
//        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
//        multipartResolver.setResolveLazily(true);//resolveLazily属性启用是为了推迟文件解析，以在在UploadAction中捕获文件大小异常
//        multipartResolver.setDefaultEncoding("UTF-8");
//        multipartResolver.setMaxUploadSize(5400000L);
//        return multipartResolver;
//    }
	public MappingJackson2HttpMessageConverter createMappingJackson2HttpMessageConverter(){
		MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
		List<MediaType> mediaTypes = new ArrayList<>();
		MediaType mediaType = new MediaType("text/html;charset=UTF-8");
		mediaTypes.add(mediaType);
		converter.setSupportedMediaTypes(mediaTypes);
		return converter;
	}
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOrigins("*", "null")
				.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
				.allowedHeaders("Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With")
				.maxAge(3600)
				.allowCredentials(true);
	}



}
